package com.atguigu.dao.impl;

import com.atguigu.dao.BookDao;
import com.atguigu.pojo.Book;

import java.util.List;
//多人合作,有人写接口,有人实现接口,有人调用接口
public class BookDaoImpl extends BaseDao implements BookDao {
    @Override
    public int addBook(Book book) {
        System.out.println("BookDaoImpl程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_book(`name`," +
                "`author`,`price`," +
                "`sales`,`stock`,`img_path`) values(?,?,?,?,?,?);";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath());
    }

    @Override
    public int deleteBookById(Integer id) {
        String sql="delete from t_book where id=?";
        return update(sql,id);
    }

    @Override
    public int updateBook(Book book) {
        String sql="update t_book set `name`=?,`author`=?,`price`=?,`sales`=?,`stock`=?,`img_path`=? where id=?";
        return update(sql,book.getName(),book.getAuthor(),book.getPrice(),book.getSales(),book.getStock(),book.getImgPath(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) {
        String sql ="select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book where id=?";
        return queryForOne(Book.class,sql,id);
    }

    @Override
    public List<Book> queryBooks() {// 查询全部
        String sql ="select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book";
        return queryForList(Book.class,sql);
    }

    @Override
    public Integer queryForPageTotalCount() {//查询总条目数,为page分片做准备
        String sql="select count(*) from t_book";
        Number bookCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return bookCount.intValue();
    }

    @Override
    public List<Book> queryForPageItems(Integer begin,Integer pageSize) {
        String sql="select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath from t_book limit ?,?";

        return queryForList(Book.class,sql,begin,pageSize);//查询数据片段
    }

    @Override
    public Integer queryForPageTotalCountByPrice(Integer min, Integer max) {
        String sql="select count(*) from t_book where price between ? and ?";
        Number bookCount=(Number) queryForSingleValue(sql,min,max);//这个语句似乎可以返回任何类型的查询
        return bookCount.intValue();
    }

    @Override
    public List<Book> queryForPageItemsByPrice(Integer begin, Integer pageSize, Integer min, Integer max) {
        String sql="select `id`,`name`,`author`,`price`,`sales`,`stock`,`img_path` imgPath " +
                "from t_book " +
                "where price between ? and ? order by price" +
                " limit ?,?";

        return queryForList(Book.class,sql,min,max,begin,pageSize);//查询数据片段
    }
}
